﻿using AnesSystem.Models;
using Microsoft.Office.Interop.Excel;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace AnesSystem.ViewModels
{
    public class TopMenuCtrlViewModel : BaseViewModel<TopMenuCtrlViewModel>
    {
        #region Variables

        private ObservableCollection<ToggleButton> _toggleBtnList = new ObservableCollection<ToggleButton>();

        private ToggleButton _preTlb;

        private bool isMouseClick = false;

        #endregion

        #region Constructor

        public TopMenuCtrlViewModel()
        {
            InitialMenu();
        }

        #endregion

        #region Properties
        public ObservableCollection<ToggleButton> ToggleBtnList
        {
            get
            {
                return _toggleBtnList;
            }

            set
            {
                _toggleBtnList = value;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        public void InitialMenu()
        {
            ResourceDictionary dic = (ResourceDictionary)System.Windows.Application.LoadComponent(new Uri(@"..\..\Resource\AnesSystem_Style_Blue.xaml", UriKind.RelativeOrAbsolute));
            var list = DataManager.Instance.MenuList.Where(O => O.Parent == 0);
            ToggleBtnList.Clear();
            foreach (var menu in list)
            {
                StackPanel stack = new StackPanel();
                ToggleButton btn = new ToggleButton();
                Image image = new Image();
                btn.Width = 120;
                btn.Height = 60;
                btn.FontSize = 20;
                btn.Content = menu.Name;
                string style = "Menu_FirstLevel_ToggleButton_" + menu.Fxid + "_Style";
                System.Windows.Style btn_style = (System.Windows.Style)dic[style];
                btn.Style = btn_style;
                btn.DataContext = menu;
                btn.Click += Btn_Click;
                btn.Unchecked += Btn_Checked;
                btn.Checked += Btn_Checked;
                ToggleBtnList.Add(btn);
            }
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            isMouseClick = true;
            ToggleButton btn = sender as ToggleButton;
            MenuListModel model = btn.DataContext as MenuListModel;
            LeftMenuCtrlViewModel.Instance.DispalyLeftMenu(model.Index);
        }

        #endregion

        #region Public/Protected Methods

        public void SetSelectMenu(int id)
        {
            foreach (var item in _toggleBtnList)
            {
                MenuListModel menu = item.DataContext as MenuListModel;
                if (menu.Index.Equals(id))
                {
                    item.IsChecked = true;
                    break;
                }
            }
        }

        #endregion

        #region EventHandlers

        private void Btn_Checked(object sender, RoutedEventArgs e)
        {
            ToggleButton btn = sender as ToggleButton;
            if (_preTlb != btn)
            {
                foreach (ToggleButton ctrl in _toggleBtnList)
                {
                    if (!btn.Equals(ctrl))
                    {
                        ctrl.Unchecked -= Btn_Checked;
                        ctrl.Checked -= Btn_Checked;
                        ctrl.IsChecked = !btn.IsChecked;
                        ctrl.Unchecked += Btn_Checked;
                        ctrl.Checked += Btn_Checked;
                    }
                }
                _preTlb = btn;
            }
            else
            {
                btn.Unchecked -= Btn_Checked;
                btn.Checked -= Btn_Checked;
                btn.IsChecked = !btn.IsChecked;
                btn.Unchecked += Btn_Checked;
                btn.Checked += Btn_Checked;
            }
        }

        #endregion
    }
}
